package com.chen.manager.demo;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;

import java.io.FileInputStream;

/**
 * @author cgj
 * @date 2/19/25 7:12 PM
 */
public class TestMinio {
    public static void main(String[] args) throws  Exception {
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint("http://140.143.124.123:9001")
                        .credentials("admin", "cgj1223.")
                        .build();

        // 判断桶存不存在
        boolean found =
                minioClient.bucketExists(BucketExistsArgs.builder().bucket("spzx-bucket").build());
        if (!found) {
            //如果不存在创建一个新的桶
            minioClient.makeBucket(MakeBucketArgs.builder().bucket("spzx-bucket").build());
        } else {
            System.out.println("已经存在桶");
        }
        FileInputStream fis = new FileInputStream("/Users/chen/Desktop/test2.jpg");
        minioClient.putObject(
                PutObjectArgs.builder().bucket("spzx-bucket").object("008.jpg").stream(
                                fis, fis.available(), -1)

                       .contentType("image/jpeg")
                        .build());
        // 构建fileUrl
        String fileUrl = "http://140.143.124.123:9001/spzx-bucket/008.jpg" ;
        System.out.println(fileUrl);

    }
}

